package com.servlet;

import com.dao.AddDao;
import com.dao.impl.AddDaoImpl;
import com.entity.Users;
import com.service.UsersService;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.EOFException;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

@WebServlet("/addUser")
public class AddUserServlet extends HttpServlet {
    private AddDao addDao;
    UsersService usersService;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        this.addDao = new AddDaoImpl();
        this.usersService = new UsersService();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/admin/add_user.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String gender = req.getParameter("gender");
        String password = req.getParameter("password");
        String birthday = req.getParameter("birthday");//获取到页面输入的时间
        String userType = req.getParameter("userType");


        Users user = new Users();
        Date date = null;
        java.sql.Date sqlDate = null;
        if (username != null && password != null) {
            user.setUsername(username);
            user.setPwdHash(password);
            user.setSex(gender);
            //格式化时间
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            try {
                date = format.parse(birthday);
                sqlDate = new java.sql.Date(date.getTime());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            user.setBirthday(sqlDate);

            if (userType == null) {
                userType = "读者";
                user.setUsertype(1L);
            }else if(userType !=null){
                switch (userType) {
                    case "读者":
                        user.setUsertype(1L);
                        break;
                    case "图书管理员":
                        user.setUsertype(2L);
                        break;
                    case "系统管理员":
                        user.setUsertype(3L);
                        break;
                    default:
                        // 处理其他情况，例如抛出异常或设置默认值
                        break;
                }
            }
            user.setIsfull("正常");
            user.setMoney(0.0);
        } else {
            List<String> errors = new ArrayList<>();
            if (username == null || password == null || sqlDate == null) {
                errors.add("还有选项没选择");
            }
            req.setAttribute("errors", errors);
            req.getRequestDispatcher("/error.jsp").forward(req, resp);
        }
        try {
            Users byUsername = usersService.getByUsername(username);
            if (Objects.nonNull(byUsername)) {//说明已有该用户，只要更新该用户信息就可以了
                byUsername = usersService.updateAdmin(user);
                req.getRequestDispatcher("/adminSearch").forward(req, resp);
            } else {//否则添加用户
                byUsername = usersService.add(user);
            }
            req.setAttribute("user", byUsername);
            req.getRequestDispatcher("/adminSearch").forward(req, resp);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
